#!/bin/bash
#: Title 	: iphone_images
#: Author	: "John Lehr" <slo.sleuth@gmail.com>
#: Date		: 05/04/2011
#: Version	: 1.0.1
#: Description	: extract EXIF/map iPhone Photos
#: Options	: None

#: 05/04/2011 : v1.0.1 Added Apple Store account info extraction
#: 05/03/2011 : v1.0.0 Initial Release

## Variables
progname="${0##*/}"
deps="exiftool"
itunes=0
map=0
gpscount=0

## Functions

usage()
{
	echo "USAGE: $progname [-ahm] [path]"
	echo "       where 'path' is the path to be searched"
	cat << EOF
	
Options (only one option may be used at a time):
	-a	extract Apple Store user information
	-h	print this help
	-m	mapping format (csv)

Options MUST preceed the path to be processed. 

Information: $progname searches a path for images or videos containing EXIF
data and dumps the information to standard output.  Optionally, a text file
containing mapping data or Apple Store account information can be dumped.

EOF
}

check_deps ()
{
	for i in $deps; do
		which $i >/dev/null
		if [ $? -gt 0 ]; then
			echo "Error: $i is not installed or is not in the path"
		fi
	done
}

get_exif ()
{
	## Export exif with exiftool
	filename="======== $i"
	meta=$(exiftool "$i")
	if [ $map = 1 ]; then
		gps=$(echo "$meta" | grep "GPS Position" | sed 's/deg//' | cut -d ':' -f2)
		if [ -n "$gps" ]; then
			if [ $gpscount -eq 0 ]; then 
				echo "name,desc,latitude,longitude"
				gpscount=1
			fi
			echo "$(basename $i),Created:$(echo "$meta" | grep -E "^Create Date" | cut -d ':' -f2-),$gps"
		fi
	elif [ $itunes = 1 ] && [[ "$filename" =~ m4v ]]; then
		echo "$filename"
		echo "$meta" | grep -E '^File Type|^Apple Store Account'
		echo -e "Apple Store Real Name\t\t: $(strings "$i" | grep -m1 name | sed 's/name//')\n"
	elif [ $itunes = 0 ]; then
		echo "$filename"
		echo -e "$meta\n"
	fi
}

notice ()
{
	echo -e "Open mapping output in mapping program or upload to http://www.gpsvisualizer.com/" >&2
}

## Check for installed dependencies
check_deps

## list of options program will accept;
## options followed by a colon take arguments
optstring=ahm

## The loop calls getops until there are no more options on the command 
## line.  Each option is stored in $opt, any option arguments are stored
## in OPTARG
while getopts $optstring opt; do
	case $opt in
		h) usage >&2; exit 0 ;;
		a) itunes=1 ;;
		m) map=1 ;;
		*) echo; usage >&2; exit 1 ;;
	esac
done

## Remove options from the command line
## $OPTIND points to the next, unparsed argument
shift "$(( $OPTIND -1 ))"

## Check for conflicting arguments
if [ $(( $map + $itunes )) -gt 1 ]; then
	echo "Error: arguments -i and -m may not be used together" >&2
	exit 1
fi

## Process file according to chosen option
## Determine path to search
path="$1"
if [ -z $path ] || [ "$path" = "." ]; then
	path="$(pwd)"
fi

## Search for files
echo "Searching \"$path\" for files..." >&2
find $path -type f |
	while read i; do
		type=$(file -bi "$i" | grep -E 'image|video')
		if [ -n "$type" ]; then
			get_exif  #process file with get_exif function
		fi
	done
	
## Print map notice
if [ $map -eq 1 ]; then
	notice
fi

exit 0
